package edu.itla.medifacil.persistence.managers;


import java.security.InvalidParameterException;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import edu.itla.medifacil.helpers.Validator;
import edu.itla.medifacil.persistence.entities.Attribute;
import edu.itla.medifacil.persistence.entities.People;
import edu.itla.medifacil.persistence.entities.PersonAttributesValue;
import edu.itla.medifacil.persistence.entities.Role;
import edu.itla.medifacil.persistence.entities.User;

public class PersonManager {
	private EntityManager em = null;
	
	public PersonManager() {
		this.em = Persistence.createEntityManagerFactory("MediFacil").createEntityManager();
	}
	
	public People save( Map<String, String> parameters, int roleId ){
		em.getTransaction().begin();
		
		People person = new People();
		User personUser = new User();
		
		String name = parameters.get("name");
		String lastname = parameters.get("lastname");
		String identification = parameters.get("identification");
		if( ! Validator.notEmpty( name, lastname, identification ) )
			throw new InvalidParameterException();
		
		
		if( parameters.get("id") != null && parameters.get("id").trim().length() > 0 ){
			person = em.find( People.class, Integer.parseInt(parameters.get("id")));
			em.createQuery("DELETE FROM PersonAttributesValue WHERE person_id =" + person.getId()).executeUpdate();
		}else{
			Role role = em.find(Role.class, roleId);
			
			personUser.setUsername( identification );
			personUser.setPassword( identification );
			personUser.setIsActive((byte)1);
			personUser.setRole(role);
			
			person.setUser( personUser );
			em.persist(personUser);
		}
		
		person.setName( name );
		person.setLastName(lastname);
		person.setIdentification(identification);
		
		em.persist(person);
		
		for( Object parameterName : parameters.keySet() ){
			if( parameterName.toString().startsWith("attributes.") ){
				int attributeId = Integer.parseInt(parameterName.toString().replaceFirst("attributes.", ""));
				Attribute attribute = em.find( Attribute.class, attributeId);
				PersonAttributesValue attributeValue = new PersonAttributesValue();
				
				attributeValue.setAttribute( attribute );
				attributeValue.setValue( parameters.get( parameterName ).toString() );
				attributeValue.setPeople(person);
				
				em.persist( attributeValue );
			}
		}
		em.getTransaction().commit();
		
		return this.find( person.getId() );
	}
	
	
	public void delete( int patientId ){
		People person = em.find( People.class , patientId);
		User user = person.getUser();
		user.setIsActive( (byte) 0 );
		em.getTransaction().begin();
		em.persist(user);
		em.getTransaction().commit();
	}

	public People find( int patientId ){
		em.clear();
		return em.find( People.class, patientId);
	}
	
	@SuppressWarnings("unchecked")
	public List<People> findAll(){
		return (List<People>) em.createQuery( "SELECT p FROM People p" ).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<People> findAll(int roleId){
		return (List<People>) em.createQuery( "SELECT p FROM People p WHERE p.user.role.id = "+ roleId ).getResultList();
	}

}
